<?php

/**
 * 短信发送平台
 * User: devforma
 * Date: 14/12/11
 * Time: 14:49
 */

class SmsSender {

    private $config, $logger;

    private static $platforms = array(
        'yunxin' => '云信',
        'qixintong' => '企信通',
    );

    function __construct($config) {
        $this->config = $config;
        $this->logger = new Logger();
    }

    /**
     * 获取支持的短信平台列表
     * @return array
     */
    public function getSupportPlatforms() {
        return array_values(self::$platforms);
    }

    /**
     * 发送短信
     * @param string $platform 短信平台
     * @param string $number 手机号
     * @param string $content 短信内容
     * @param bool $isVoice 是否为语音短信
     * @return bool 是否发送成功
     */
    public function sendMsg($platform, $number, $content, $isVoice = false) {
        if ($isVoice) { //语音验证码
            preg_match('/(\d+)/', $content, $matches);
            $result = $this->sendLMMsg($number, $matches[1], true);
            if ($result && $result->g_SubmitResult->State == 0) {
                return true;
            } else {
                $this->logger->i('sms_lm_voice_fail:'.$result->g_SubmitResult->State);
            }

        } elseif ($platform == 'yunxin') {
            $result = $this->sendYunxinMsg($number, $content);
            if ($result && strpos($result->SendMsgResult, '-') === false) { //发送成功
                return true;
            } else {
                $this->logger->i('sms_yunxin_fail:'.$number.':'.$result->SendMsgResult);
            }
        } elseif ($platform == 'lm') {
            $result = $this->sendLMMsg($number, $content);
            if ($result && $result->g_SubmitResult->State == 0) {
                return true;
            } else {
                $this->logger->i('sms_lm_fail:'.$result->g_SubmitResult->State);
            }
        }

        return false;
    }

    /**
     * 云信短信平台
     * @param string $number 手机号
     * @param string $content 短信内容
     * @return mixed 接口请求结果
     */
    private function sendYunxinMsg($number, $content) {
        $client = new SoapClient($this->config['yunxin_soap']);

        $param = array(
            "userCode"=>$this->config['yunxin_usercode'],
            "userPass"=>$this->config['yunxin_userpass'],
            "DesNo"=>$number,
            "Msg"=>$content,
            "Channel"=>"0"
        );

        return $client->__soapCall('SendMsg', array('parameters'=>$param));
    }

    /**
     * Lmobile短信平台
     * @param $number
     * @param $content
     * @param bool $isVoice
     * @return mixed
     */
    private function sendLMMsg($number, $content, $isVoice = false) {
        $client = new SoapClient($this->config['lm_soap']);

        $param = array(
            "sname"     => $this->config['lm_sname'],
            "spwd"      => $this->config['lm_spwd'],
            "scorpid"   => $this->config['lm_scorpid'],
            "sprdid"    => $isVoice ? $this->config['lm_voice_sprdid'] : $this->config['lm_sprdid'],
            "sdst"      => $number,
            "smsg"      => $content
        );

        return $client->__soapCall('g_Submit', array('parameters'=>$param));
    }
}
